package demo04;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hp on 2020/2/26.
 */

public class Test01 {
    static List<Teacher> teachers = new ArrayList<>();
    static List<Phone> phones = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        //运行xml()时吧data.xml文件删除
        //xml();
        chuanjian();
        liu();
        maxPrice();
    }

    public static void maxPrice() {
        System.out.println("手机最贵的是：");
        int max = 0;
        for (Teacher teacher : teachers) {
            if (teacher.getPhone().getPrice() > max){
                max = teacher.getPhone().getPrice();
            }
        }
        for (Teacher teacher : teachers) {
            if (teacher.getPhone().getPrice() == max){
                System.out.println(teacher.getName());
            }
        }
    }

    public static void liu() {
        System.out.println("刘某人的手机型号是：");
        for (Teacher teacher : teachers) {
            if (teacher.getName().endsWith("刘某人")) {
                System.out.println(teacher.getPhone().getBrand());
                break;
            }
        }
    }

    public static void chuanjian() throws Exception {
        Element rootElement = new SAXReader().read("data2.xml").getRootElement();
        List<Element> bean = rootElement.elements("bean");
        for (Element element : bean) {
            if (element.attributeValue("className").endsWith("Teacher")) {
                List<Element> person = element.elements("person");
                for (Element person1 : person) {
                    Teacher teacher = new Teacher();
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("type", person1.attributeValue("type"));
                    map.put("name", person1.attributeValue("name"));
                    map.put("age", person1.attributeValue("age"));
                    BeanUtils.populate(teacher, map);
                    teachers.add(teacher);
                }
            } else {
                List<Element> phone = element.elements("phone");
                for (Element element1 : phone) {
                    Phone phone1 = new Phone();
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("owner", element1.attributeValue("owner"));
                    map.put("brand", element1.attributeValue("brand"));
                    map.put("price", element1.attributeValue("price"));
                    BeanUtils.populate(phone1, map);
                    phones.add(phone1);
                }
            }
        }
        for (Teacher teacher : teachers) {
            for (Phone phone : phones) {
                if (phone.getOwner().endsWith(teacher.getName())) {
                    teacher.setPhone(phone);
                    break;
                }
            }
        }
        System.out.println(teachers);
    }

    public static void xml() throws IOException {
        for (int i = 0; i < 5; i++) {
            List<String> strings = FileUtils.readLines(new File(i + "data.split"), "utf-8");
            for (int j = 0; j < strings.size(); j++) {
                if (j == strings.size() - 1) {
                    FileUtils.write(new File("data2.xml"), strings.get(j), "utf-8", true);
                } else {
                    FileUtils.write(new File("data2.xml"), strings.get(j) + "\r\n", "utf-8", true);
                }
            }
        }
    }
}
